//
//  SDNavigationController.swift
//  shudong
//
//  Created by luojie on 2018/3/4.
//  Copyright © 2018年 luojie. All rights reserved.
//

import UIKit

class SDNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //隐藏默认的NavigationBar
        navigationBar.isHidden = true
    }
   
    //重写push方法，所有的push动作都会调用此方法
    //
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(viewController)
        //如果不是栈底控制器才需要隐藏，根控制器不需要处理
        if childViewControllers.count>0 {
            //隐藏除首页之外的tabbar
            viewController.hidesBottomBarWhenPushed = true
            
            // 判断控制器类型
            if let vc=viewController as?SDBaseViewController{
                var title="返回"
                //判断控制器的级数，只有一个字控制器的时候，现实栈底控制器的标题
                if childViewControllers.count==1{
                    //title 是首页的标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                //取出自定义的navItem
                vc.navItem.leftBarButtonItem=UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack:true)
            }
        }
       
        
        super.pushViewController(viewController,animated: true)
        
    }
    func popToParent(){
        popViewController(animated: true)
    }
}
